package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userDto) {
        //1.设置PageHelper分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        //2.调用mapper接方法，执行查询
        List<User> UserList = userMapper.selectList(userDto);
        //3.解析并封装成PageResult返回分页结果
        Page<User> page = (Page<User>) UserList;
        return new PageResult<>(page.getTotal(), page.getResult());

    }

    @Override
    public void add(User user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setCreateTime(LocalDateTime.now());
        String password = (user.getUsername() + "123");
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.insert(user);
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.delete(ids);

    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public List<User> listByRole(String roleLabel) {

        return  userMapper.listByRole(roleLabel) ;
    }

    @Override
    public LoginResultVo login(User user) {
       User u =  userMapper.selectUserName(user.getUsername());
       if(u == null){
           log.info("用户不存在");
           throw new BusinessException("用户不存在");
       }
       if(!u.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
           log.info("密码错误");
           throw new BusinessException("密码错误");
       }
       if (u.getStatus() != 1){
           log.info("用户被禁用");
           throw new BusinessException("用户被禁用");
       }
       LoginResultVo loginResultVo = new LoginResultVo();
       loginResultVo.setId(u.getId());
       loginResultVo.setUsername(u.getUsername());
       loginResultVo.setName(u.getName());
       loginResultVo.setImage(u.getImage());
       loginResultVo.setRoleLabel(u.getRoleLabel());
       loginResultVo.setToken(" ");


       //生成令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String  jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        log.info("生成令牌：{}", loginResultVo.getToken());


       return loginResultVo;


    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }


}
